/*
 * COPYRIGHT:	2010 Christopher Smith, licenced under the GNU GPL v2; see COPYING in the top level directory
 * PROJECT:		sdedit
 * FILE:		winhelp cpp.cpp
 * PURPOSE:		C++ and SEH support for common Win32 operations
 *				
 */

void SetListBoxItemText(HWND Wnd, int Index, PCWSTR NewText)
{
	LRESULT selected = RaiseSendMessageListBox(Wnd, LB_GETSEL, Index, NULL);

	RaiseSendMessageListBox(Wnd, LB_DELETESTRING, Index, NULL);
	RaiseSendMessageListBox(Wnd, LB_INSERTSTRING, Index, (LPARAM)NewText);

	if(selected)
	{
		if(GetWindowLong(Wnd, GWL_STYLE) & LBS_MULTIPLESEL)
			RaiseSendMessageListBox(Wnd, LB_SETSEL, TRUE, Index);
		else
			RaiseSendMessageListBox(Wnd, LB_SETCURSEL, Index, NULL);
	}
}

RECT GetListBoxItemScreenRect(HWND ListWnd, ULONG Row)
{
	RECT itemrect, listrect, rect;

	RaiseSendMessageListBox(ListWnd, LB_GETITEMRECT, Row, (LPARAM)&itemrect);
	GetWindowRect(ListWnd, &listrect);

	rect.left = itemrect.left + listrect.left;
	rect.top = itemrect.top + listrect.top;
	rect.right = itemrect.right + listrect.left + 1;
	rect.bottom = itemrect.bottom + listrect.top + 1;

	return rect;
}

auto_take<WCHAR> GetNewListBoxText(HWND ListBox, ULONG_PTR Index)
{
	ULONG_PTR textlen;

	textlen = RaiseSendMessageListBox(ListBox, LB_GETTEXTLEN, Index, NULL);

	auto_szpod<WCHAR> text = auto_szpod_empty_ct(textlen + 1);

	RaiseSendMessageListBox(ListBox, LB_GETTEXT, Index, c_cast<LPARAM>(text.get()));

	return text.release();
}

auto_take<WCHAR> GetNewWindowText(HWND Wnd)
{
	LRESULT TextLen = SendMessage(Wnd, WM_GETTEXTLENGTH, 0, 0) + 1;

	auto_szpod<WCHAR> Text(auto_szpod_empty_ct(TextLen));

	GetWindowText(Wnd, Text, static_cast<int>(TextLen));

	return Text.release();
}

void free_ExecutiveHandle(HANDLE Handle)
{
	NtClose(Handle);
}

typedef auto_free<VOID, &free_ExecutiveHandle> ExecutiveHandle;

